namespace Xmef.Library
{
    using System;

    public static class ThreadHelper
    {
        public class ActionDelegateWorkItem<T> : SequentialWorkItemContainer<T>
        {
            private Action<T> _process;

            public ActionDelegateWorkItem(T item, Action<T> process) : base(item)
            {
                this._process = process;
            }

            protected override void Process()
            {
                this._process(GetWorkItem());
            }

            public static void QueryAsync(T item, Action<T> process)
            {
                SequentialWorkItemContainer<T>.QueryAsync(new ThreadHelper.ActionDelegateWorkItem<T>(item, process));
            }

            public static void QuerySync(T item, Action<T> process)
            {
                SequentialWorkItemContainer<T>.QuerySync(new ThreadHelper.ActionDelegateWorkItem<T>(item, process));
            }
        }

        public class PredicateDelegateWorkItem<T> : SequentialWorkItemContainer<T>
        {
            private bool _match;
            private Predicate<T> _process;

            public PredicateDelegateWorkItem(T item, Predicate<T> process) : base(item)
            {
                this._process = process;
            }

            protected override void Process()
            {
                this._match = this._process(GetWorkItem());
            }

            public static void QueryAsync(Predicate<T> process, T item)
            {
                SequentialWorkItemContainer<T>.QueryAsync(new ThreadHelper.PredicateDelegateWorkItem<T>(item, process));
            }

            public bool Match
            {
                get
                {
                    return this._match;
                }
            }
        }
    }
}

